package com.englisheveryday.media;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.media.SimpleMediaPlayerService.ServiceBinder;
import com.englisheveryday.utils.L;

/**
 * <pre>
 *      自定义播放器类
 * </pre>
 * @author 马永华
 * @date 2012-9-12 下午5:15:50
 * @version 1.1
 */

public class SimpleMediaPlayer extends LinearLayout {
	
	private Context context;
	private ImageView play,pre,next,playmode;
	private boolean isPaying;
	private OnPlayerClickListener onPreClickListener;
	private OnPlayerClickListener onNextClickListener;
	private Context applicationContext;
	private String playId;
	private Intent intent;
	private SimpleMediaPlayerStateReceiver receiver;
	private SharedPreferences sharedPreferences;
	private Application a;
	public SimpleMediaPlayer(Context context) {
		super(context);
	}
	
	public SimpleMediaPlayer(Context context,AttributeSet attributeSet){
		super(context,attributeSet);
		L.d(context.toString());
		this.context = context;
		isPaying = false;
	}
	
	public void init(){
		L.d("初始化");
		//启动服务
		intent = new Intent(applicationContext,SimpleMediaPlayerService.class);
		L.d(applicationContext.toString());
		applicationContext.startService(intent);
		receiver = new SimpleMediaPlayerStateReceiver();
		applicationContext.registerReceiver(receiver, new IntentFilter("com.englisheveryday.mediaplayer.info"));
		sharedPreferences = applicationContext.getSharedPreferences(Constants.SETTING_PLAYER, Context.MODE_PRIVATE);
		View view = LayoutInflater.from(context).inflate(R.layout.simple_media_player,this,true);
	    play = (ImageView)view.findViewById(R.id.play);
		pre = (ImageView)view.findViewById(R.id.pre);
		next = (ImageView)view.findViewById(R.id.next);
		playmode = (ImageView)view.findViewById(R.id.playmode);
		//刷新模式
		refreshPlayMode();
		pre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(onPreClickListener!=null)
				onPreClickListener.onPlayerClick();
				applicationContext.bindService(intent,new PlayServiceConnection("",Constants.PLAYER_OPERATE_PRE),Context.BIND_AUTO_CREATE);
			}
		});
		next.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(onNextClickListener!=null)
				onNextClickListener.onPlayerClick();
				applicationContext.bindService(intent,new PlayServiceConnection("",Constants.PLAYER_OPERATE_NEXT),Context.BIND_AUTO_CREATE);
			}
		});
		play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isPaying)
				applicationContext.bindService(intent,new PlayServiceConnection("",Constants.PLAYER_OPERATE_PAUSE),Context.BIND_AUTO_CREATE);
				else if(!isPaying)
				applicationContext.bindService(intent,new PlayServiceConnection("",Constants.PLAYER_OPERATE_RESTART),Context.BIND_AUTO_CREATE);
			}
		});
		playmode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int mode = sharedPreferences.getInt("mode",Constants.PLAYER_MODE_ORDER);
				switch(mode) {
				case  Constants.PLAYER_MODE_ORDER : {
					//如果当前是ORDER 则变为 singler
					playmode.setImageDrawable(applicationContext.getResources().getDrawable(R.drawable.lyric_simpleplayer_mode_singler_selector));
					Editor editor = sharedPreferences.edit();
					editor.putInt("mode", Constants.PLAYER_MODE_SINGLER);
					editor.commit();
				}
				break;
				case Constants.PLAYER_MODE_SINGLER : {
					//如果当前为singler 则变为order
					playmode.setImageDrawable(applicationContext.getResources().getDrawable(R.drawable.lyric_simpleplayer_mode_order_selector));
					Editor editor = sharedPreferences.edit();
					editor.putInt("mode", Constants.PLAYER_MODE_ORDER);
					editor.commit();
				}
				break;
				default : {
					
				}
				}
			}
		});
		
	}
	
	public void onStopService(){
		applicationContext.stopService(intent);
	}
	
	public void refreshPlayMode(){
		int mode = sharedPreferences.getInt("mode",Constants.PLAYER_MODE_ORDER);
		switch(mode) {
		case  Constants.PLAYER_MODE_ORDER : {
			playmode.setImageDrawable(applicationContext.getResources().getDrawable(R.drawable.lyric_simpleplayer_mode_order_selector));
		}
		break;
		case Constants.PLAYER_MODE_SINGLER : {
			playmode.setImageDrawable(applicationContext.getResources().getDrawable(R.drawable.lyric_simpleplayer_mode_singler_selector));
		}
		break;
		default : {
			
		}
		}
	}
	
	
	//播放
	public void play(){
		//播放
		applicationContext.bindService(intent,new PlayServiceConnection(playId,Constants.PLAYER_OPERATE_PLAY),Context.BIND_AUTO_CREATE);
	}
	
	public void onDestroy(){
		applicationContext.unregisterReceiver(receiver);
	}
	
	private class PlayServiceConnection implements ServiceConnection{
		
		private String id;
		private int type;
		
		public PlayServiceConnection(String id,int type){
			this.id = id;
			this.type = type;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			ServiceBinder serviceBinder = (ServiceBinder)service;
			SimpleMediaPlayerService sps = serviceBinder.getService();
			L.d("type"+type);
			sps.play(id, type);
			applicationContext.unbindService(this);			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
		L.d("disconnected");
			
		}
		
	}
	
	
	/**
	 * 播放类的监听器
	 * @author cheney
	 *
	 */
	public interface OnPlayerClickListener{
		public void onPlayerClick();
	}

	public OnPlayerClickListener getOnPreClickListener() {
		return onPreClickListener;
	}

	public void setOnPreClickListener(OnPlayerClickListener onPreClickListener) {
		this.onPreClickListener = onPreClickListener;
	}

	public OnPlayerClickListener getOnNextClickListener() {
		return onNextClickListener;
	}

	public void setOnNextClickListener(OnPlayerClickListener onNextClickListener) {
		this.onNextClickListener = onNextClickListener;
	}

	public String getPlayId() {
		return this.playId;
	}

	public void setPlayId(String playId) {
		this.playId = playId;
	}

	public Context getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(Context applicationContext) {
		this.applicationContext = applicationContext;
	}

	class SimpleMediaPlayerStateReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context arg0, Intent intent) {
			switch(intent.getIntExtra("type",-1)){
			case Constants.PLAYER_OPERATE_PLAY : {
				isPaying = true;
				play.setImageDrawable(getResources().getDrawable(R.drawable.lyric_simpleplayer_pause_selector));
			}
			break;
			case Constants.PLAYER_OPERATE_PAUSE : {
				isPaying = false;
				play.setImageDrawable(getResources().getDrawable(R.drawable.lyric_simpleplayer_play_selector));
			}
			break;
			case Constants.PLAYER_OPERATE_STOP : {
				isPaying = false; 
				play.setImageDrawable(getResources().getDrawable(R.drawable.lyric_simpleplayer_play_selector));
			}
			break;
			default :{
				
			}
			}
		}
		
	}
}

 